package com.javaweb.controller

import com.javaweb.pojo.SysUser
import com.javaweb.service.SysUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.stereotype.Controller

/**
 * Controller模板
 * @author Siaze
 * @date 2012/12/11
 */
@Controller
class BaseController {
    /**
     * sysUserService是sys_user表（用户表）查询工具，可以通过自定义查询条件查询数据
     */
    @Autowired
    var sysUserService: SysUserService? = null

    /**
     * 获取当前用户的id
     */
    fun getSysUserId(): Long {
        return (SecurityContextHolder.getContext().authentication.principal as UserDetails).username.toLong()
    }

    /**
     * 获取当前用户的对象，包括sys_user里的信息
     */
    fun getUser(): SysUser? {
        /*
            通过调用上面的方法获取id，再使用sysUserService获取sysUser对象
         */
        return sysUserService?.getById(getSysUserId())
    }
}